/**
 * Created by mike on 2017/5/15.
 */


var http = require("http");
var express = require("express"); //引入express
var socketIo = require("socket.io"); //引入socket.io


var app = new express();

app.use('/static', express.static('public'));

var server = http.createServer(app);
var io = new socketIo(server); //将socket.io注入express模块

//客户端 1 的访问地址
app.get("/msg", function (req, res, next) {
    res.sendFile(__dirname + "/views/msg.html");
});

app.get("/msg_01", function (req, res, next) {
    res.sendFile(__dirname + "/views/msg_01.html");
});

app.get("/msg_02", function (req, res, next) {
    res.sendFile(__dirname + "/views/msg_02.html");
});

app.get("/slide_show_01", function (req, res, next) {
    res.sendFile(__dirname + "/views/slide_show_01.html");
});

app.get("/slide_show_02", function (req, res, next) {
    res.sendFile(__dirname + "/views/slide_show_02.html");
});
app.get("/slide_show_all", function (req, res, next) {
    res.sendFile(__dirname + "/views/slide_show_all.html");
});

app.get("/draw", function (req, res, next) {
    res.sendFile(__dirname + "/views/draw.html");
});

app.get("/draw_client", function (req, res, next) {
    res.sendFile(__dirname + "/views/draw_client.html");
});



app.get("/event", function (req, res, next) {
    res.sendFile(__dirname + "/views/event.html");
});


app.get("/teach_app", function (req, res, next) {
    res.sendFile(__dirname + "/views/teach_app.html");
});


console.log(33333333333333333);

server.listen(8888); //express 监听 8080 端口，因为本机80端口已被暂用

var socketMap = {};
var guidSocketMap = {};
var teacher_socket_guid = '';

//每个客户端socket连接时都会触发 connection 事件
io.on("connection", function (clientSocket) {

    var guid = new Date().getTime();

    socketMap[clientSocket.id] = clientSocket;
    guidSocketMap[guid] = clientSocket;

    //第一次推送信息 分配标识
    clientSocket.emit("register_server_to_app", {
        header: {
            guid: guid
        },
        body: {}
    });

    console.log(guidSocketMap, "=========guidSocketMap=======");

    // 默认消息通道 
    clientSocket.on("teach_app_to_server", function (data) {
        // 使用 emit 发送消息，broadcast 表示 除自己以外的所有已连接的socket客户端。

        console.log(clientSocket.id);
        console.log(teacher_socket_guid);

        if (data.header.guid == teacher_socket_guid) {
            // io.sockets.broadcast.emit("teach_server_to_app", data);

            clientSocket.broadcast.emit("teach_server_to_app", data);
        }

        // io.sockets.emit("teach_server_to_app", data);

    });

    //监听客户端发送的 申请主控 事件
    clientSocket.on("apply_to_teacher", function (data) {
        // 使用 emit 发送消息，broadcast 表示 除自己以外的所有已连接的socket客户端。
        // clientSocket.broadcast.emit("receiveMsg", data);

        if (data.body.content == "888") {

            teacher_socket_guid = data.header.guid;

            // io.sockets.emit("teach_server_to_app", {
            //     body: {
            //         type: 'to_be_teacher_success',
            //         content: teacher_socket_guid
            //     }
            // });

            //告诉所有人吧  让受控和锁屏
            io.sockets.emit("teach_server_to_app", {
                body: {
                    type: 'to_be_teacher_success',
                    content: teacher_socket_guid
                }
            });

        } else {
            clientSocket.emit("teach_server_to_app", {
                body: {
                    type: 'notice_error',
                    content: '申请失败,原因:密码错误'
                }
            });

        }

    });

    clientSocket.on("release_teacher", function (data) {

        if (teacher_socket_guid = data.header.guid) {
            io.sockets.emit("teach_server_to_app", {
                body: {
                    type: 'release_teacher',
                    content: teacher_socket_guid
                }
            });
        }

    })






    // io.sockets.emit("teach_server_to_app", {
    //     header: {
    //         guid: new Date().getTime(),
    //     },
    //     body: {}
    // });

});